package com.itheima.controller;

import com.itheima.pojo.Feedback;
import com.itheima.pojo.User;
import com.itheima.service.FeedbackService;
import com.itheima.service.UserService;
import com.itheima.utils.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/oauth")
public class OauthController {

    @Resource
    private FeedbackService feedbackService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private UserService userService;

    /**
     * 提交反馈
     *
     * @Param feedback
     */
    @PostMapping("/addFeedback")
    public Result addFeedback(@RequestBody Feedback feedback) {
        Integer count = feedbackService.addFeedback(feedback);
        if (count > 0) {
            return new Result<>(StatusCode.SUCCESS, "提交反馈成功");
        } else {
            return new Result<>(StatusCode.SUCCESS, "提交反馈失败");
        }
    }

    /**
     * 退出登录，删除accessToken
     */
    @PostMapping("/delete/{accessToken}")
    public Result loginOut(@PathVariable String accessToken) {
        System.out.println(accessToken);
        if(accessToken != null && !accessToken.equals("")){
            accessToken = accessToken.substring(7);
        }
        String username = JwtTokenUtils.getUsername(accessToken);
        redisTemplate.delete(username);
        return new Result<>(StatusCode.SUCCESS, "退出登录成功");
    }

    /**
     * 编辑用户信息
     *
     * @Param user
     */
    @PostMapping("/editUser")
    public Result editUser(@RequestBody User user) {
        Integer count = userService.editUser(user);
        if (count > 0) {
            return new Result<>(StatusCode.SUCCESS, "更新成功");
        } else {
            return new Result<>(StatusCode.SUCCESS, "更新失败");
        }
    }

    /**
     * 获取用户反馈
     */
    @GetMapping("/getFeedbackList")
    public Result getFeedbackList(HttpServletRequest request) {
        String username = CookieUtils.getCookieValue(request, CookieUtils.LOGIN_USER);
        List<Feedback> feedbackList = feedbackService.getFeedbackList(username);
        return new Result<>(StatusCode.SUCCESS, feedbackList);
    }

    /**
     * 更新用户密码
     */
    @PostMapping("/updateUserPwd")
    public Result updateUserPwd(@RequestParam String oldPwd, @RequestParam String newPwd, HttpServletRequest request) {
        String username = CookieUtils.getCookieValue(request, CookieUtils.LOGIN_USER);
        User user = userService.findByUsername(username);
        if(!MD5Utils.md5(oldPwd).equals(user.getPassWord())){
            return new Result<>(StatusCode.SUCCESS,"密码错误");
        }
        user.setPassWord(MD5Utils.md5(newPwd));
        userService.edit(user);
        return new Result<>(StatusCode.SUCCESS, "密码修改成功");
    }

    /**
     * 获取用户信息
     *
     */
    @GetMapping("/verify/{accessToken}")
    public Result verify(@PathVariable String accessToken) {
        System.out.println("aaaaaaaaaaaaaa"+accessToken);
        if(accessToken != null && !accessToken.equals("")){
            accessToken = accessToken.substring(7);
        }
        String username = JwtTokenUtils.getUsername(accessToken);
        User user = userService.findByUsername(username);
        return new Result<>(StatusCode.SUCCESS, user);
    }

}
